#include "common.h"
namespace common {
void build(std::vector<int> &a, int root, int length) {

    int left = root << 1 | 1;
    int right = left + 1;
    int min_index = right;
    if (min_index < length) {
        if (a.at(min_index) > a.at(left))
            min_index = left;
        if (a.at(min_index) < a.at(root)) {
            std::swap(a.at(min_index), a.at(root));
            build(a, min_index, length);
        }
    }
}
void min_heap(std::vector<int> &a) {
    for (int i = a.size() >> 1; i >= 0; i--)
        build(a, i, a.size());
}
std::vector<int> getLeastNumbersMin(std::vector<int> &a, int k) {
    std::vector<int> heap_vec;
    int i = 0;
    for (; i < k; i++)
        heap_vec.push_back(a[i]);
    min_heap(heap_vec);
    for (; i < a.size(); i++) {
        if (a[i] > heap_vec[0]) {
            heap_vec[0] = a[i];
            min_heap(heap_vec);
        }
    }
    return heap_vec;
}
std::vector<int> getLeastNumbers(std::vector<int> &arr, int k) {
    std::priority_queue<int> pq;
    std::vector<int> result;
    for (int i = 0; i < arr.size(); i++) {
        pq.push(-arr[i]);
    }
    for (int i = 0; i < k; i++) {
        result.push_back(-pq.top());
        pq.pop();
    }
    return result;
}
} // namespace common
